package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.MediaTypeImpl;
import com.shisfish.library.Model.MediaType;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;

@Path("mediaType")
public class MediaTypeController extends GlobalVariable {

	private MediaTypeImpl mediaTypeImpl = new MediaTypeImpl();
	
	@POST
	@Path("insertAMediaType/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertAMediaType(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@FormParam("name") String name, @FormParam("fcode") int fcode) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			MediaType mediaType = new MediaType(name, fcode);
			int flag = mediaTypeImpl.insertOne(mediaType);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateAMediaType/{idadmin}/{code}")
	@Produces("application/json; charset=utf-8")
	public String updateAMediaType(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@FormParam("name") String name, @FormParam("fcode") int fcode, 
			@PathParam("code") int code) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			List<MediaType> mediaTypes = mediaTypeImpl.selectById(code);
			
			if (mediaTypes.size() > 0) {
				MediaType mediaType = mediaTypes.get(0);
				mediaType.setName(name);
				mediaType.setFcode(fcode);
				int flag = mediaTypeImpl.updateOne(mediaType);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("deleteAMediaType/{idadmin}/{code}")
	@Produces("application/json; charset=utf-8")
	public String deleteAMediaType(@PathParam("idadmin") String idadmin, @Context HttpServletRequest request, 
			@PathParam("code") int code) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		if (Login.isLoginAdmin(idadmin, request)) {
			int flag = mediaTypeImpl.deleteOneById(code);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, UPDATE_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByFcode/{fcode}")
	@Produces("application/json; charset=utf-8")
	public String selectByFcode(@PathParam("fcode") int fcode) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<MediaType> mediaTypes = mediaTypeImpl.selectByFcode(fcode);
		if (mediaTypes.size() > 0) {
			globalMap.put(STATUS_CODE, SUCCESS);
			globalMap.put("list", mediaTypes);
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByCode/{code}")
	@Produces("application/json; charset=utf-8")
	public String selectByCode(@PathParam("code") int code) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		List<MediaType> mediaTypes = mediaTypeImpl.selectById(code);
		if (mediaTypes.size() > 0) {
			globalMap.put(STATUS_CODE, SUCCESS);
			globalMap.put("obj", mediaTypes.get(0));
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
}
